function o(o, n, t, i) {
    wx.hideLoading();
    var e = "无";
    i.length && (e = i.join("\n")), wx.showModal({
        title: "成功" + o + "项，失败" + n + "项",
        content: "失败信息:\n" + e,
        showCancel: !1,
        success: function(o) {
            o.confirm && t && t();
        }
    });
}

function n(o, n, t, i) {
    wx.downloadFile({
        url: n,
        success: function(n) {
            200 === n.statusCode ? "video" === o ? (console.log(n.tempFilePath), wx.saveVideoToPhotosAlbum({
                filePath: n.tempFilePath,
                success: function(o) {
                    t && t();
                },
                fail: function(o) {
                    i && i("保存失败");
                }
            })) : "image" === o ? wx.saveImageToPhotosAlbum({
                filePath: n.tempFilePath,
                success: function(o) {
                    t && t();
                },
                fail: function(o) {
                    i && i("保存失败");
                }
            }) : "file" === o && wx.saveFile({
                tempFilePath: n.tempFilePath,
                success: function(o) {
                    o.savedFilePath;
                    console.log(o);
                }
            }) : i && i("状态码非200");
        },
        fail: function(o) {
            console.log(o), i && i("下载失败" + JSON.stringify(o));
        }
    });
}

function t(o) {
    wx.showLoading({
        title: "检查授权情况",
        mask: !0
    }), wx.getSetting({
        success: function(n) {
            wx.hideLoading(), n.authSetting["scope.writePhotosAlbum"] ? o && o() : wx.authorize({
                scope: "scope.writePhotosAlbum",
                success: function() {
                    o && o();
                },
                fail: function() {
                    wx.showModal({
                        title: "",
                        content: "保存到系统相册需要授权",
                        confirmText: "授权",
                        success: function(n) {
                            n.confirm && wx.openSetting({
                                success: function(n) {
                                    !0 === n.authSetting["scope.writePhotosAlbum"] && o && o();
                                }
                            });
                        },
                        fail: function() {
                            wx.showToast({
                                title: "打开设置页失败",
                                icon: "none"
                            });
                        }
                    });
                }
            });
        },
        fail: function() {
            wx.hideLoading(), wx.showToast({
                title: "获取授权失败",
                icon: "none"
            });
        }
    });
}

var i = function(o) {
    return (o = o.toString())[1] ? o : "0" + o;
};

module.exports = {
    formatTime: function(o) {
        var n = o.getFullYear(), t = o.getMonth() + 1, e = o.getDate(), s = o.getHours(), c = o.getMinutes(), u = o.getSeconds();
        return [ n, t, e ].map(i).join("/") + " " + [ s, c, u ].map(i).join(":");
    },
    downloadFile: function(o, i, e, s) {
        t(function() {
            wx.showLoading({
                title: "正在下载",
                mask: !0
            }), n(o, i, function() {
                wx.hideLoading(), wx.showToast({
                    title: "下载成功",
                    icon: "none"
                }), e && e();
            }, function(o) {
                wx.hideLoading(), wx.showToast({
                    title: o,
                    icon: "none"
                }), s && s();
            });
        });
    },
    downloadFiles: function(i, e, s) {
        var c = 0, u = 0, a = e.length, l = [];
        t(function() {
            wx.showLoading({
                title: "正在下载",
                mask: !0
            });
            for (var t = 0; t < e.length; t++) !function(t) {
                n(i, e[t], function() {
                    ++c + u === a && o(c, u, s, l);
                }, function(n) {
                    u++, n && l.push("视频" + t + n), c + u === a && o(c, u, s, l);
                });
            }(t);
        });
    }
};